package com.aaa.dem1102.d03;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/11/2  10:32
 */
public class Test {
    public static void main(String[] args) throws Exception {

        /*
        * 第一种方法
        * */
        //创建一个ca77ab1e类型的任务对象
        MyCallable myCallable = new MyCallable();
        //把任务返回的结果也封装到FutureTask对象中
        FutureTask futureTask = new FutureTask(myCallable);
        //创建一个线程对象，把上面的任务交于该线程对象。
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());


        /*
         * 第二种方法
         * */
//        ExecutorService executorService = Executors.newFixedThreadPool(2);
//        Future<Integer> future = executorService.submit(new MyCallable());
//        executorService.shutdown();
//        System.out.println(future.get());
    }
}

class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        int sum=0;
        for (int i = 0; i <= 100; i++) {
            sum +=i;
        }
        return sum;
    }
}